package com.ys.servers.user.service.impl;

import com.ys.servers.common.data.UserType;
import com.ys.servers.common.util.CommonUtils;
import com.ys.servers.common.util.TimeUtils;

import com.ys.servers.user.dao.UserSignInfoDao;
import com.ys.servers.user.dao.impl.UserSignInfoDaoImpl;
import com.ys.servers.user.data.UserSignInfo;
import com.ys.servers.user.service.UserSignInfoService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Future;

/**
 * Created by taotao on 2017/5/24.
 */
public class UserSignInfoServiceImpl implements UserSignInfoService {

    private UserSignInfoDao userSignInfoDao;

    public UserSignInfoDao getUserSignInfoDao() {
        return userSignInfoDao;
    }

    public UserSignInfoServiceImpl setUserSignInfoDao(UserSignInfoDao userSignInfoDao) {
        this.userSignInfoDao = userSignInfoDao;
        return this;
    }

    public UserSignInfoServiceImpl() {
        setUserSignInfoDao(new UserSignInfoDaoImpl());
    }

    @Override
    public UserSignInfoService userSign(String userId, UserType userType, Handler<AsyncResult<UserSignInfo>> resultHandler) {

        getUserSignInfo(userId, userType, getUserSignInfoRes -> {
            if (CommonUtils.validAsyncResult(getUserSignInfoRes)) {

                UserSignInfo userSignInfo = getUserSignInfoRes.result();
                if (TimeUtils.today(userSignInfo.getLastSignInTime())) {
                    //今天已经签到过
                } else if (!TimeUtils.continuousDay(userSignInfo.getLastSignInTime())) {
                    //签到断了
                } else {
                    //正常签到
                    userSignInfo.setLastSignInTime(System.currentTimeMillis()).setContinuousDay(userSignInfo.getContinuousDay() + 1);
                    //
                    getUserSignInfoDao().userSign(userSignInfo).setHandler(res -> {
                        if (CommonUtils.validAsyncResult(res)) {
                            //加积分

                          /*  EventBusService.<UserPointsService>getProxy(discovery, new JsonObject().put("name", SERVICE_NAME), ar -> {
                                if (ar.succeeded()) {
                                    MyService myService = ar.result();
                                    myService.retrieveData(...);
                                }
                            });*/

                        } else {
                            //签到失败
                        }
                    });

                }
            } else {

            }
        });
        return this;
    }

    @Override
    public UserSignInfoService getUserSignInfo(String userId, UserType userType, Handler<AsyncResult<UserSignInfo>> resultHandler) {

        getUserSignInfoDao().getUserSignInfo(userId, userType).setHandler(res -> {
            if (CommonUtils.validAsyncResult(res)) {
                resultHandler.handle(res);
            } else {
                long now = System.currentTimeMillis();
                UserSignInfo userSignInfo = new UserSignInfo().setFirstSignInTime(0)
                        .setLastSignInTime(0).setUserId(userId).setContinuousDay(1).setUserType(userType);
                getUserSignInfoDao().userSign(userSignInfo).setHandler(resultHandler);
            }
        });

        return this;
    }
}
